'use strict';//js严格模式
var orgLocation=orgLocation||{};
orgLocation= {
	$modal:$('#editLocation'),
     $deleteLocation :$("#deleteLocation"),
     urlBase :null,
     url:null,
     clickType:null,
     locationId:null,
     tr:null,
     ids:[],
     init:function(urlBase,pageSize,totalPage){
     	this.urlBase=urlBase;
     	this.pageSize=pageSize;
     	this.totalPage=totalPage;
     	pager.init(orgLocation.pageSize,orgLocation.totalPage,function(num){
            $("#listBody").children().remove();                    
            orgLocation.getPage(num, pageSize);
        });
     	$("#btnQuery").on("click",function(){
     		$.ajax({
                url : orgLocation.urlBase + "/orgLocation/search",
                type : 'POST',
                dataType : 'json',
                data : {
                    name : $("#queryName").val()
                },
                cache : false
            }).done(function (result) {
            	if (result) {
            		$("#listBody").children().remove();
            		pager.init(result.pageSize,result.totalPage,function(num){
            			  $("#listBody").children().remove();                    
            	            orgLocation.getPage(num, result.pageSize);
            		});
            		 for (var i = 0; i < result.list.length; i++) {
                     	orgLocation.addTr(result.list[i]);
                     };
                     $('.am-dropdown').dropdown();
                     orgLocation.setCbAllStatus();
				}               
            }).fail(function () {
                console.log("error");
            });        
     	});
        $("#cbAll").on("change", function () {
        	var checked=$(this).is(":checked");
            $("input[name=cbSub]").prop("checked", checked);
            $("#modalMultiDelete").prop("disabled",!checked);
        });
        $("body").on("change", "input[name=cbSub]", function () {
            orgLocation.setCbAllStatus();
        });
        $("body").on("click", ".edit", function () {
            $("#modalTitle").text("编辑机构地区");
            orgLocation.clickType = "edit";
            orgLocation.url = orgLocation.urlBase + "/orgLocation/edit";
            orgLocation.tr = $(this).parent().parent().parent().parent().parent();
            var remark = orgLocation.tr.children().eq(-2).text();
            $("#locRemark").val(remark);
            var location = orgLocation.tr.children().eq(2).text();
            $("#locName").val(location);
            orgLocation.locationId = orgLocation.tr.children().eq(1).text();
            orgLocation.$modal.modal({});
        });
        $("#modalMultiDelete").on('click', function (event) {
            orgLocation.url = orgLocation.urlBase + "/orgLocation/delete";
            $("#deleteTitle").text('你确定要删除这些地区记录吗？')
            orgLocation.ids = [];
            $("input[name=cbSub]:checked").each(function (index, el) {
                orgLocation.ids.push($(this).parent().next().text());
            });
            if (orgLocation.ids.length<=0) {
            	return;
            };
            orgLocation.$deleteLocation.modal({
                onConfirm : function () {
                    $.ajax({
                        cache : false,
                        type : "post",
                        url : orgLocation.url,
                        data : {
                            ids : orgLocation.ids.toString()
                        }
                    }).done(function (result) {
                        if (result.status == "success") {
                            $("input[name=cbSub]:checked").each(function (index, el) {
                                $(this).parent().parent().remove()
                            });
                            orgLocation.setCbAllStatus();
                            alert("批量删除成功！");
                        } else {
                            alert("批量删除失败！");
                        }
                    }).fail(function () {
                        alert("error");
                    });
                }
            });
        });
        $("body").on("click", ".delete", function () {
            orgLocation.url = orgLocation.urlBase + "/orgLocation/deleteById";
            var me = $(this);
            $("#deleteTitle").text('你确定要删除这条地区记录吗？')
            orgLocation.$deleteLocation.modal({
                onConfirm : function () {
                    orgLocation.tr = me.parent().parent().parent().parent().parent();
                    orgLocation.locationId = orgLocation.tr.children().eq(1).text();
                    $.ajax({
                        cache : false,
                        type : "post",
                        url : orgLocation.url,
                        data : {
                            id : orgLocation.locationId
                        }
                    }).done(function (result) {
                        if (result.status == "success") {
                        	orgLocation.tr.remove();
                            orgLocation.setCbAllStatus();
                            alert("删除成功！");
                        } else {
                            alert("删除失败！");
                        }
                    }).fail(function () {
                        alert("error");
                    });
                }
            });
        });
        $("#modalAdd").on("click", function () {
            $("#modalTitle").text("新增机构地区");
            orgLocation.clickType = "add";
            orgLocation.url = orgLocation.urlBase + "/orgLocation/add";
            $("#locRemark").val("");
            $("#locName").val("");
            orgLocation.locationId = null;
            orgLocation.$modal.modal({});
        });
        $("#btnSubmit").on("click", function () {
            $.ajax({
                cache : false,
                type : "post",
                url : orgLocation.url,
                data : {
                    id : orgLocation.locationId,
                    name : $("#locName").val(),
                    remark : $("#locRemark").val()
                }
            }).done(function (result) {
                if (orgLocation.clickType == "edit") {
                    if (result.status == "success") {
                        orgLocation.tr.children().eq(-2).text($("#locRemark").val());
                        orgLocation.tr.children().eq(2).text($("#locName").val());
                        orgLocation.$modal.modal('close');
                        alert("修改成功！");
                    } else {
                        alert("修改失败！");
                    }
                } else {
                    if (result.status == "success") {
                        orgLocation.$modal.modal('close');
                        var newTr = $('<tr><td><input type="checkbox" name="cbSub" /></td><td>'
                                +result.message.id
                                 + '</td><td>'
                                +$("#locName").val()
                                 + '</td><td>'
                                +result.message.appid
                                 + '</td><td>'
                                +$("#locRemark").val()
                                 + '</td><td><div class="am-dropdown" data-am-dropdown><button class="am-btn am-btn-default am-btn-xs am-dropdown-toggle"  data-am-dropdown-toggle><span class="am-icon-cog"></span><span class="am-icon-caret-down"></span></button><ul class="am-dropdown-content">  <li><a href="javascript:void(0);" class="edit">编辑</a></li><li><a href="javascript:void(0);" class="delete">删除</a></li></ul></div></td></tr>');
                        $("#listBody").append(newTr);
                        orgLocation.setCbAllStatus();
                        $('.am-dropdown').dropdown();
                        alert("新增成功！");
                    } else {
                        alert("新增失败！");
                    }
                }
            }).fail(function () {
                alert("error");
            });
        });
     },
     setCbAllStatus:function(){
     	var b = true;
        $("input[name=cbSub]").each(function () {
            if ($(this).is(":checked") == false) {
                b = false;
                return false;
            }
        });
        $("#cbAll").prop("checked", b);
        $("#modalMultiDelete").prop("disabled",b)
    },
    addTr:function(item){
    	var newTr = $('<tr><td><input type="checkbox" name="cbSub" /></td><td>'
                +item.id
                 + '</td><td>'
                +item.name
                 + '</td><td>'
                +item.appid
                 + '</td><td>'
                +item.remark
                 + '</td><td><div class="am-dropdown" data-am-dropdown><button class="am-btn am-btn-default am-btn-xs am-dropdown-toggle"  data-am-dropdown-toggle><span class="am-icon-cog"></span><span class="am-icon-caret-down"></span></button><ul class="am-dropdown-content">  <li><a href="javascript:void(0);" class="edit">编辑</a></li><li><a href="javascript:void(0);" class="delete">删除</a></li></ul></div></td></tr>');
        $("#listBody").append(newTr);
    },
    getPage:function(pageNumber, pageSize){
    	$.ajax({
            url : orgLocation.urlBase + "/orgLocation/page",
            type : 'POST',
            dataType : 'json',
            data : {
                pageNumber : pageNumber,
                pageSize : pageSize
            },
            cache : false
        }).done(function (result) {
            for (var i = 0; i < result.list.length; i++) {
            	orgLocation.addTr(result.list[i]);
            };
            $('.am-dropdown').dropdown();
            orgLocation.setCbAllStatus();
        }).fail(function () {
            console.log("error");
        });
    }        	
};